﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using TOOLS;
namespace KMServer
{
    public partial class MainForm : Form
    {
        ClientHandle c = new ClientHandle();
        public MainForm()
        {
            InitializeComponent();
        }
      

        private void btStart_Click(object sender, EventArgs e)
        {
            if (btStart.Text == "启动服务")
            {
                c.Start();
                tMsgProc.Enabled = true;
                btStart.Text = "停止服务";
                this.timerUpdateLoginStatus.Enabled = true;
            }
            else
            {
                c.Stop();
                tMsgProc.Enabled = false;
                btStart.Text = "启动服务";
                this.timerUpdateLoginStatus.Enabled = false;
            }
        }
     
        private void tMsgProc_Tick(object sender, EventArgs e)
        {
            if (cbEnableLog.Checked)
            {
                bool flag = false;
                if (c.clientLogQueue.Count > 0)
                {
                    flag = true;
                    txMsg.Text += c.clientLogQueue.Dequeue() + "\r\n";

                }
                string temp = BLL.BaseBll.DeLog();
                if (temp != null)
                {
                    flag = true;
                    txMsg.Text += temp + "\r\n";
                }
                if (flag)
                {
                    if (txMsg.Text.Where(p => p == '\r').Count() > 1000)
                    {
                        txMsg.Text = txMsg.Text.Substring(500);
                        txMsg.Text = txMsg.Text.Substring(txMsg.Text.IndexOf('\n') + 1);
                    }

                    txMsg.Focus();//获取焦点
                    txMsg.Select(this.txMsg.TextLength, 0);//光标定位到文本最后
                    txMsg.ScrollToCaret();
                }
               
               
            }
           
        }

        private void timerUpdateLoginStatus_Tick(object sender, EventArgs e)
        {
            BLL.BaseBll.CheckSigTimeOut();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
            if (File.Exists("./loginInfo.login"))
            {
                string objstring = File.ReadAllText("./loginInfo.login");
                List<KeyValuePair<string, Model.LoginInfo>> dd = objstring.JsonToObj<List<KeyValuePair<string, Model.LoginInfo>>>();
                if (dd != null)
                {
                    foreach (var item in dd)
                    {
                        BLL.BaseBll.loginUserList.Add(item.Key,item.Value);
                    }
                }
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {

            File.WriteAllText("./loginInfo.login", BLL.BaseBll.loginUserList.ToList().ToJSON());
        }
    }
}
